package com.kehutong.admin.entity;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.coraframework.json.annotation.JSONField;
import org.coraframework.orm.Entity;
import org.coraframework.orm.annotation.Column;
import org.coraframework.orm.annotation.UnSave;

import com.kehutong.common.entity.Root;


public abstract class TreeResource<T extends TreeResource<T>> extends Entity implements Comparable<T> {
	
	private static final long serialVersionUID = -6824994638168652739L;

	protected TreeResource(String id) {
		super(id);
	}
	
	@Column(defValue = "NOT NULL", comment = "名称")
	private String name;
	
	@Column(defValue = "NOT NULL", comment = "父级编号")
	private String parent_id;
	
	@Column(defValue = "NOT NULL", comment = "所有父级编号")
	private List<String> parent_ids;
	
	@JSONField(serialize=false)
	@Column(comment = "修改者")
	private String update_by;

	@JSONField(serialize=false)
	@Column(comment = "创建者")
	private String create_by;
	
	@JSONField(serialize=false)
	@Column(comment = "逻辑删除")
	private boolean deleted;
	
	@UnSave private T parent;
	@UnSave private List<T> childList;


	public String getParent_id() {
		return parent_id;
	}

	public void setParent_id(String parent_id) {
		this.parent_id = parent_id;
	}

	public List<String> getParent_ids() {
		return parent_ids;
	}

	public void setParent_ids(List<String> parent_ids) {
		this.parent_ids = parent_ids;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUpdate_by() {
		return update_by;
	}

	public void setUpdate_by(String update_by) {
		this.update_by = update_by;
	}

	public String getCreate_by() {
		return create_by;
	}

	public void setCreate_by(String create_by) {
		this.create_by = create_by;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public boolean isRoot(){
		return Root.isRoot(this.id);
	}
	
	public abstract boolean sortAble();

	public T getParent() {
		return parent;
	}

	public void setParent(T parent) {
		this.parent = parent;
	}

	public List<T> getChildList() {
		if (childList == null) {
			return Collections.emptyList();
		}
		
		return childList;
	}

	public void setChildList(List<T> childList) {
		this.childList = childList;
	}

	public void addChild(T child) {
		if (childList == null) {
			childList = new LinkedList<>();
		}
		childList.add(child);
	}

	@Override
	public int compareTo(T o) {
		return 0;
	}
}
